package GUI.Productos;

import java.rmi.RemoteException;
import java.util.ArrayList;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.jface.layout.TableColumnLayout;
import org.eclipse.swt.widgets.Button;

import article.entities.ArticleVO;
import article.entities.IngredientVO;
import service.ServiceFacade;
import service.article.exceptions.ProxyException;
import service.article.interfaces.ArticleProxyMgt;

import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class modAtrib extends Dialog {

	protected Object result;
	protected Shell shell;
	private Table table;
	private Table table_1;
	ArticleVO articulo;

	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public modAtrib(Shell parent, int style, ArticleVO art) {
		super(parent, style);
		setText("Seleccione los Ingredientes del Articulo");
		articulo=art;
	}

	/**
	 * Open the dialog.
	 * @return the result
	 * @throws ProxyException 
	 * @throws RemoteException 
	 */
	public Object open() throws RemoteException, ProxyException {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 * @throws ProxyException 
	 * @throws RemoteException 
	 */
	private void createContents() throws RemoteException, ProxyException {
		shell = new Shell(getParent(), getStyle());
		shell.setSize(643, 394);
		shell.setText(getText());
		shell.setLayout(new FormLayout());
		ArticleProxyMgt uso = ServiceFacade.getInstance().getArticleProxyMgt();
		final IngredientVO [] ings = uso.getAllIngredients();
		//System.out.println("numero de ingredientes" + ings.length);
		
		
		
		
		table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
		FormData fd_table = new FormData();
		fd_table.left = new FormAttachment(0, 10);
		fd_table.bottom = new FormAttachment(100, -82);
		fd_table.right = new FormAttachment(0, 252);
		table.setLayoutData(fd_table);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		
		table_1 = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
		table_1.setLinesVisible(true);
		table_1.setHeaderVisible(true);
		FormData fd_table_1 = new FormData();
		fd_table_1.bottom = new FormAttachment(table, 0, SWT.BOTTOM);
		fd_table_1.top = new FormAttachment(0, 32);
		fd_table_1.right = new FormAttachment(100, -24);
		table_1.setLayoutData(fd_table_1);
		
		Label lblIngredientes = new Label(shell, SWT.NONE);
		fd_table.top = new FormAttachment(lblIngredientes, 8);
		FormData fd_lblIngredientes = new FormData();
		fd_lblIngredientes.bottom = new FormAttachment(100, -348);
		fd_lblIngredientes.left = new FormAttachment(0, 98);
		lblIngredientes.setLayoutData(fd_lblIngredientes);
		lblIngredientes.setText("Ingredientes ");
		
		Label lblIngredientesDelArticulo = new Label(shell, SWT.NONE);
		FormData fd_lblIngredientesDelArticulo = new FormData();
		fd_lblIngredientesDelArticulo.bottom = new FormAttachment(lblIngredientes, 0, SWT.BOTTOM);
		fd_lblIngredientesDelArticulo.right = new FormAttachment(100, -108);
		lblIngredientesDelArticulo.setLayoutData(fd_lblIngredientesDelArticulo);
		lblIngredientesDelArticulo.setText("Ingredientes del Articulo");
		
		Button btnAgregar = new Button(shell, SWT.NONE);
		btnAgregar.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				TableItem[] item = table.getSelection();
				TableItem t = new TableItem(table_1, SWT.NONE);
				t.setText(0, item[0].getText(0));
				
				table.remove(table.getSelectionIndex());
			}
		});
		fd_table_1.left = new FormAttachment(btnAgregar, 21);
		
		TableColumn tblclmnNewColumn_1 = new TableColumn(table_1, SWT.NONE);
		tblclmnNewColumn_1.setWidth(239);
		tblclmnNewColumn_1.setText(" ");
		FormData fd_btnAgregar = new FormData();
		fd_btnAgregar.right = new FormAttachment(100, -287);
		fd_btnAgregar.left = new FormAttachment(table, 19);
		fd_btnAgregar.top = new FormAttachment(0, 86);
		btnAgregar.setLayoutData(fd_btnAgregar);
		btnAgregar.setText("Agregar");
		
		Button btnQuitar = new Button(shell, SWT.NONE);
		btnQuitar.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				TableItem[] item = table_1.getSelection();
				TableItem t = new TableItem(table, SWT.NONE);
				t.setText(0, item[0].getText(0));
				
				table_1.remove(table_1.getSelectionIndex());
			}
		});
		FormData fd_btnQuitar = new FormData();
		fd_btnQuitar.right = new FormAttachment(btnAgregar, 0, SWT.RIGHT);
		fd_btnQuitar.top = new FormAttachment(btnAgregar, 36);
		fd_btnQuitar.left = new FormAttachment(btnAgregar, 0, SWT.LEFT);
		btnQuitar.setLayoutData(fd_btnQuitar);
		btnQuitar.setText("Quitar");
		
		Button btnAceptar = new Button(shell, SWT.NONE);
		btnAceptar.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				ArrayList<IngredientVO> ingredientes = new ArrayList<IngredientVO>() ;
				TableItem[] items = table_1.getItems();
				
				for (int ii = 0; ii<items.length; ii ++){
					for (int j=0;j<ings.length;j++){
						if(ings[j]!=null){
						if (items[ii].getText().equalsIgnoreCase(ings[j].getIngr())){
							
					ingredientes.add(ings[j]);
						}}}}
				
				IngredientVO[] i= new IngredientVO[ingredientes.size()];
				for(int p=0;p<ingredientes.size();p++){
					i[p]=ingredientes.get(p);
					System.out.println("p + " + p + ingredientes.get(p).getIngr());
				} 
				
				articulo.setIngr(i);
				VerProductoMod m = new VerProductoMod (shell, shell.getStyle(), articulo);
				shell.setVisible(false);
				m.open();
						}
					
			
		});
		
		FormData fd_btnAceptar = new FormData();
		fd_btnAceptar.top = new FormAttachment(table, 24);
		
		TableColumn tblclmnNewColumn = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn.setWidth(241);
		tblclmnNewColumn.setText(" ");
		
	//	TableItem tableItem = new TableItem(table, SWT.NONE);
		
		fd_btnAceptar.left = new FormAttachment(0, 140);
		btnAceptar.setLayoutData(fd_btnAceptar);
		btnAceptar.setText("Aceptar");
		
		Button btnCancelar = new Button(shell, SWT.NONE);
		FormData fd_btnCancelar = new FormData();
		fd_btnCancelar.bottom = new FormAttachment(btnAceptar, 0, SWT.BOTTOM);
		fd_btnCancelar.left = new FormAttachment(btnAceptar, 159);
		btnCancelar.setLayoutData(fd_btnCancelar);
		btnCancelar.setText("Cancelar");
		for(int i=0;i<ings.length;i++){
			TableItem tableItem = new TableItem(table, SWT.NONE);
			tableItem.setText(ings[i].getIngr());
		}
	}
}
